﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace V5_EaspLibs.Core
{
    /// <summary>
    /// 发送邮件类
    /// </summary>
    public class EmailHelper
    {
        private static readonly EmailHelper m_Instance = new EmailHelper();
        /// <summary>
        /// 
        /// </summary>
        public static EmailHelper Instance {
            get { return m_Instance; }
        }
        private  string _SMTP;
        /// <summary>
        /// 邮件服务器
        /// </summary>
        public string SMTP
        {
            get { return _SMTP; }
            set { _SMTP = value; }
        }
        private string _EMAIL;
        /// <summary>
        /// 邮件发送者邮箱
        /// </summary>
        public string EMAIL
        {
            get { return _EMAIL; }
            set { _EMAIL = value; }
        }

        private string _EPASS;
        /// <summary>
        /// 邮件发送者邮箱密码
        /// </summary>
        public string EPASS
        {
            get { return _EPASS; }
            set { _EPASS = value; }
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="SendTo">收件人，多个用逗号隔开</param>
        /// <param name="MailTitle">邮件标题</param>
        /// <param name="MailBody">邮件内容，自行Server.HtmlEncode处理</param>
        /// <returns></returns>
        public bool SendUserMail(string SendTo, string MailTitle, string MailBody)
        {
            bool strResult = false;
            try
            {
                string smtp = SMTP;
                string from = EMAIL;
                string fromPass = EPASS;
                SmtpClient client = new SmtpClient(smtp);
                client.Credentials = new System.Net.NetworkCredential(from, fromPass);
                client.Timeout = 10000;
                client.EnableSsl = false;

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, SendTo, MailTitle, MailBody);
                message.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                message.IsBodyHtml = true;
                client.Send(message);
                strResult = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strResult;
        }
    }
}
